//
//  SNewsMyCollectionViewCell.m
//  News
//
//  Created by dlios on 15-5-14.
//  Copyright (c) 2015年 PEAK. All rights reserved.
//

#import "SNewsMyCollectionViewCell.h"

#define kNewsMyTableViewCell @"123"
#define kNewsMyTwoTableViewCell @"234"
#define kNewsMyThreeTableViewCell @"345"
#define kNewsMyFourTabelViewCell @"456"

@interface SNewsMyCollectionViewCell()<UITableViewDataSource,UITableViewDelegate>


@property(nonatomic ,retain)UITableViewCell *cell;

@property(nonatomic ,retain)NSMutableArray *xiangqingIdZongArr;




@end

@implementation SNewsMyCollectionViewCell

-(void)dealloc
{
    
    [_model release];
    [_modelOne release];
    [_modelTwo release];
    [_modeleThree release];
    [_tableVIew release];
    [_TouTiao release];
    [_TouTiaoStrOne release];
    [_MutableArrOne release];
    [_ZongArr release];
    [_cell release];
    [_xiangqingIdZongArr release];
    [super dealloc];
    
}

/**
 *  重写初始化方法
 *
*/
-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
        self.tableVIew = [[UITableView alloc] init];
        _tableVIew.rowHeight = 150;
        _tableVIew.dataSource = self;
        _tableVIew.delegate = self;
        [self.contentView addSubview:_tableVIew];
        [_tableVIew release];
        self.ZongArr = [NSMutableArray array];
        
        self.xiangqingIdZongArr = [NSMutableArray array];
        
   
/**
*  注册
*/
        [_tableVIew registerClass:[SNewsMyTableViewCell class] forCellReuseIdentifier:kNewsMyTableViewCell];
        
        [_tableVIew registerClass:[SNewsMyTwoTableViewCell class] forCellReuseIdentifier:kNewsMyTwoTableViewCell];
        
        [_tableVIew registerClass:[SNewsMyThreeTableViewCell class] forCellReuseIdentifier:kNewsMyThreeTableViewCell];
        
        [_tableVIew registerClass:[SNewsMyFourTableViewCell class] forCellReuseIdentifier:kNewsMyFourTabelViewCell];

        self.i = 40;
        self.isUpLoading = NO;
        

        
        [self addHeader];
        [self addFooter];

      

    }
    return self;
    
}

-(void)addHeader
{
    
    
    
[self .tableVIew addHeaderWithCallback:^{

 
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    [center postNotificationName:@"color" object:@"123" userInfo:@{@"abcd":@"123"}];
    self.isUpLoading = NO;
    
    [self GetDataOne];
 
}];
    


}




#pragma mark - 上啦加载更多
- (void)addFooter
{
 
        
   
    //添加上啦刷新尾部控件
    [self.tableVIew addFooterWithCallback:^{
        //进入刷新状态就会回调这个block
        self.isUpLoading = YES;
        //标记为上啦操作
     
            self.i = _i + 20;

        [self GetDataOneaddFooter];

}];

}

-(void)suiBian
{

    [self.tableVIew headerBeginRefreshing];

}

-(void)GetDataOneaddFooter
{
    if (_i < 220) {
   
    
        self.TouTiaoStrOne = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/headline/%@/%ld-20.html",
                              _TouTiao,(long)_i];
    

    /**
     * 解析网址
     */
        [SNetWorkHandle getDataWithURL:_TouTiaoStrOne completion:^(id result) {
            /**
             *  总数组
             */
            NSArray * tempArr = [result objectForKey:_TouTiao];
    
    
            if (self.isUpLoading == NO) {
                [self.ZongArr removeAllObjects];
            }
            /**
             *  创建数组存储model类
             */

            /**
             *遍历所有的字典数组
             */
            for (NSDictionary * temp in tempArr) {
    
    
                if ([[temp allKeys] containsObject:@"hasCover"]) {
    
    
                    SNewsOne *NewsModelOne = [[SNewsOne alloc] initWithDic:temp];
                    [_ZongArr addObject:NewsModelOne];
    
    
                    NSString * SkipIDStr = NewsModelOne.skipID;
    
    
                    if (SkipIDStr.length == 0) {
    
                        [_xiangqingIdZongArr addObject:NewsModelOne.docid];
    
                    }else
    
                        if (SkipIDStr.length != 0 && [SkipIDStr containsString:@"|"]){
    
                            [_xiangqingIdZongArr addObject:NewsModelOne.skipID];
    
                        }else if (SkipIDStr.length != 0 && ![SkipIDStr containsString:@"|"]){
    
                            [_xiangqingIdZongArr addObject:NewsModelOne.docid];
                        }
    

    
                }

    
    
                if ([[temp allKeys] containsObject:@"imgextra"] && ![[temp allKeys] containsObject:@"hasCover"])
                {
    
                    SNews *NewsModelPic = [[SNews alloc] initWithDic:temp];
                    [_ZongArr addObject:NewsModelPic];
    
                    NSString * SkipIDStr =NewsModelPic.skipID;
    
                    if (SkipIDStr.length == 0) {
    
                        [_xiangqingIdZongArr addObject:NewsModelPic.docid];
    
                    }else if (SkipIDStr.length != 0 && [SkipIDStr containsString:@"|"]){
    
    
                        [_xiangqingIdZongArr addObject:NewsModelPic.skipID];
                    }else if (SkipIDStr.length != 0 && ![SkipIDStr containsString:@"|"])
                    {
                        [_xiangqingIdZongArr addObject:NewsModelPic.docid];
                    }
    
    
    
                }
    
                else
    
                    if (![[temp allKeys] containsObject:@"TAG"]) {
                        SNwesThree * NewsModelQuTong = [[SNwesThree alloc] initWithDic:temp];
                        [_ZongArr addObject:NewsModelQuTong];
    
                        NSString * SkipIDStr =NewsModelQuTong.skipID;
    
    
                        if (SkipIDStr.length == 0) {
    
                            [_xiangqingIdZongArr addObject:NewsModelQuTong.docid];
    
                        }else if (SkipIDStr.length != 0 && [SkipIDStr containsString:@"|"]){
    
    
                            [_xiangqingIdZongArr addObject:NewsModelQuTong.skipID];
                        }else if (SkipIDStr.length != 0 && ![SkipIDStr containsString:@"|"]){
                            [_xiangqingIdZongArr addObject:NewsModelQuTong.docid];
                        }
                        
                    }
                
            }
    
      
    /**
     *  得到所有对象数组
     */
    
                    [self.tableVIew footerEndRefreshing];//结束底部刷新效果

    /**
     *  刷新视图
     */
    
    
    
            [_tableVIew reloadData];
    
        }];
    }else {
        [self.tableVIew footerEndRefreshing];//结束底部刷新效果

    }
    
}



/**
 *  设置自定义contentViewCell位置信息
 *
 */
-(void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    
    _tableVIew.frame = CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height);
  
}


-(void)GetDataOne
{  
  
     self.TouTiaoStrOne = [NSString stringWithFormat:@"http://c.m.163.com/nc/article/headline/%@/0-20.html",
                          _TouTiao];

    /**
     * 解析网址
    */
        [self.tableVIew headerEndRefreshing];

    [SNetWorkHandle getDataWithURL:_TouTiaoStrOne completion:^(id result) {
        /**
         *  总数组
         */
    NSArray * tempArr = [result objectForKey:_TouTiao];


        if (self.isUpLoading == NO) {
            [self.ZongArr removeAllObjects];
    } 
        /**
         *  创建数组存储model类
         */

        
        self.ZongArr = [NSMutableArray array];
   
        self.xiangqingIdZongArr = [NSMutableArray array];
        
        
        /**
         *遍历所有的字典数组
         */
        for (NSDictionary * temp in tempArr) {


            if ([[temp allKeys] containsObject:@"hasCover"]) {
                
                
                SNewsOne *NewsModelOne = [[SNewsOne alloc] initWithDic:temp];
                [_ZongArr addObject:NewsModelOne];
                
                
                NSString * SkipIDStr = NewsModelOne.skipID;
                
                
                if (SkipIDStr.length == 0) {
                    
                    [_xiangqingIdZongArr addObject:NewsModelOne.docid];
                    
                }else
                    
                    if (SkipIDStr.length != 0 && [SkipIDStr containsString:@"|"]){
                        
                        [_xiangqingIdZongArr addObject:NewsModelOne.skipID];
                        
                    }else if (SkipIDStr.length != 0 && ![SkipIDStr containsString:@"|"]){
                        
                        [_xiangqingIdZongArr addObject:NewsModelOne.docid];
                    }
                
               
            }

                if ([[temp allKeys] containsObject:@"imgextra"] && ![[temp allKeys] containsObject:@"hasCover"])
            {
                
                SNews *NewsModelPic = [[SNews alloc] initWithDic:temp];
                [_ZongArr addObject:NewsModelPic];
                
                NSString * SkipIDStr =NewsModelPic.skipID;
               
                if (SkipIDStr.length == 0) {
                    
                     [_xiangqingIdZongArr addObject:NewsModelPic.docid];
                    
                }else if (SkipIDStr.length != 0 && [SkipIDStr containsString:@"|"]){
                    
               
                    [_xiangqingIdZongArr addObject:NewsModelPic.skipID];
                }else if (SkipIDStr.length != 0 && ![SkipIDStr containsString:@"|"])
                {
                    [_xiangqingIdZongArr addObject:NewsModelPic.docid];
                }
               
              }

        else
            
   if (![[temp allKeys] containsObject:@"TAG"]) {
                SNwesThree * NewsModelQuTong = [[SNwesThree alloc] initWithDic:temp];
                [_ZongArr addObject:NewsModelQuTong];
                
                NSString * SkipIDStr =NewsModelQuTong.skipID;

                
                if (SkipIDStr.length == 0) {
                    
                    [_xiangqingIdZongArr addObject:NewsModelQuTong.docid];
                    
                }else if (SkipIDStr.length != 0 && [SkipIDStr containsString:@"|"]){
                    
                    
                    [_xiangqingIdZongArr addObject:NewsModelQuTong.skipID];
                }else if (SkipIDStr.length != 0 && ![SkipIDStr containsString:@"|"]){
                    [_xiangqingIdZongArr addObject:NewsModelQuTong.docid];
                }
                
            }
           
        }
//    }
    
                /**
         *  得到所有对象数组
         */

        [self.tableVIew headerEndRefreshing];//结束头部刷新效果
        /**
         *  刷新视图
         */

        [_tableVIew reloadData];
       
    }];

}

/**
 *  tableView协议方法
 *
 */
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return _ZongArr.count;
   
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    //取消选中阴影效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSString * IdStr = _xiangqingIdZongArr[indexPath.row];
    
    if ([IdStr containsString:@"|"]) {
        
                SXiangqingTwoViewController * xiangping = [[SXiangqingTwoViewController alloc] init];
        xiangping.PicStr = IdStr;
        
                [self.ViewController.navigationController pushViewController:xiangping animated:YES];
        }
        else if (![IdStr containsString:@"|"])
        {
           
                    SXiangqingViewController * xiangping = [[SXiangqingViewController alloc] init];
            xiangping.IdStr = IdStr;
            
            
                    [self.ViewController. navigationController pushViewController:xiangping animated:YES];
    
        }
    
}


/**
 *  TouTiaoSet方法
 *
*/

-(void)setTouTiao:(NSString *)TouTiao
{
    if (TouTiao != _TouTiao) {
        [_TouTiao release];
        _TouTiao = [TouTiao retain];
        
    }

    [self GetDataOne];
   
    
}



-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if ([self.ZongArr[indexPath.row] isKindOfClass:[SNewsOne class]]) {
        SNewsMyFourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kNewsMyFourTabelViewCell];
        
        cell.modelOne = self.ZongArr[indexPath.row];
        
        return cell;
        
    } else if ([self.ZongArr[indexPath.row] isKindOfClass:[SNewsTwo class]]) {
        SNewsMyThreeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kNewsMyThreeTableViewCell];
        cell.modelTwo = self.ZongArr[indexPath.row];
        
        return cell;
    } else if ([self.ZongArr[indexPath.row] isKindOfClass:[SNwesThree class]]) {
        SNewsMyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kNewsMyTableViewCell];
        cell.modelThree = self.ZongArr [indexPath.row];
        
        return cell;
    } else {
        SNewsMyTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kNewsMyTwoTableViewCell];
        
        cell.model = self.ZongArr[indexPath.row];
        
        return cell;
    }

}



@end